﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;

public partial class User_dang_ky : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CheckEmail()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_ForgotPassword";
        Dal.ParameterCollection = new string[1] { "@UserEmail"};
        Dal.ValueCollection = new string[1] { txtEmail.Text};
        DataRow dr = Dal.GetDataRow();
        string matkhau=dr["MatKhau"].ToString();
        if (dr["UserEmail"].ToString() != "")
        {
            string mailbody = "";
            mailbody += "<b>Mật khẩu của bạn là: "+matkhau+"<br/>";
            mailbody += "Vui lòng click vào <a href='http://phoamthuc.net'>Đây</a> để trở lại Phố Ẩm Thực";
            SendMail("210.245.90.160", 25, txtEmail.Text, "info@phoamthuc.net", "Khôi phục mật khẩu", mailbody, "info@phoamthuc.net", "iloveyou");
            txtEmail.Text = "";
            lblInfo.Text = "Mật khẩu đã được gửi tới email của bạn, vui lòng check mail.";
        }

    }
    public static bool SendMail(String SMTPServer, int SMTPPort, String MailTo, String MailFrom, String MailSubject, String MailBody, String MailCredentials, String PasswordCredentials)
    {
        bool SendOK = false;
        try
        {
            if (!String.IsNullOrEmpty(MailTo) && !String.IsNullOrEmpty(MailFrom))
            {
                SmtpClient smtp = new SmtpClient(SMTPServer, SMTPPort);
                smtp.Credentials = new System.Net.NetworkCredential(MailCredentials, PasswordCredentials);
                MailAddress to = new MailAddress(MailTo);
                MailAddress from = new MailAddress(MailFrom);
                MailMessage message = new MailMessage(from, to);
                message.Subject = MailSubject;
                message.Body = MailBody;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;
                smtp.Send(message);
                SendOK = true;
            }
        }
        catch (System.Exception objEx)
        {
            SendOK = false;
        }
        return SendOK;
    }

    protected void btnGui_Click(object sender, EventArgs e)
    {
        CheckEmail();
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        txtEmail.Text = "";
    }
}
